package com.NPD.unimelb.balloongame;

import java.util.HashMap;
import java.util.Timer;
import java.util.TimerTask;

import com.NPD.unimelb.balloongame.po.Balloon;
import com.NPD.unimelb.balloongame.po.User;
import com.sun.javafx.scene.layout.region.Margins.Converter;









import android.R;
//import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.os.Message;
import android.text.Layout;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class BalloonGame extends Activity{
	private GameSurfaceView game_sv;
	
	public int SCREENWIDTH;
	public int SCREENHEIGHT;
	public int LEVEL;
	public User user;
	SoundPool sp;
	HashMap<Integer, Integer> spMap;
	//public boolean isTouched = false;
	@Override
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		Intent intent = getIntent();
		user = (User) intent.getSerializableExtra("user");
		
		//LEVEL = user.getBalloon_level();
		LEVEL = intent.getIntExtra("LEVEL", LEVEL);
		game_sv = new GameSurfaceView(BalloonGame.this);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		
		setContentView(game_sv);
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		SCREENWIDTH = dm.widthPixels;
		SCREENHEIGHT = dm.heightPixels;
		//initSoundPool();

		
	}
	

	public void backToBalloonView() {
		Intent backToBalloonView = new Intent();
		backToBalloonView.setClass(BalloonGame.this, BallGroupView.class);
		Bundle bundle = new Bundle();
		bundle.putSerializable("user", user);
		bundle.putInt("LEVEL", LEVEL);
		backToBalloonView.putExtras(bundle);
		startActivity(backToBalloonView);
		finish();
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		boolean isTouched = this.game_sv.onTouchEvent(event);
		if(game_sv.nextlevel == true){
			//user.setBalloon_level(LEVEL);
			Intent backtoShowLevel = new Intent();
			backtoShowLevel.setClass(BalloonGame.this, BalloonGame.class);
			Bundle bundle = new Bundle();
			bundle.putSerializable("user", user);
			bundle.putInt("LEVEL", LEVEL);
			backtoShowLevel.putExtras(bundle);
			startActivity(backtoShowLevel);
			finish();
		}
		return isTouched;
		
	}
	
	
	@Override
	public boolean onKeyDown(int KeyCode, KeyEvent event){
		if(KeyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
		{
			AlertDialog isExit = new AlertDialog.Builder(this).create();
			isExit.setTitle("Exit Balloon Game");
			isExit.setMessage("Do You Really Want To Exit?");
			isExit.setButton("YES", listener);
			isExit.setButton2("NO", listener);
			isExit.show();
			
		}
		return true;
	}
	DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
		@Override
		public void onClick(DialogInterface dialog, int which)
		{
			switch (which) {
			case AlertDialog.BUTTON_POSITIVE:
/*				Intent backToBalloonView = new Intent();
				backToBalloonView.setClass(BalloonGame.this, BalloonView.class);
				Bundle bundle = new Bundle();
				bundle.putSerializable("user", user);
				bundle.putInt("LEVEL", LEVEL);
				backToBalloonView.putExtras(bundle);
				startActivity(backToBalloonView);*/
				finish();
				
				break;
			case AlertDialog.BUTTON_NEGATIVE:
				break;
			default:
				break;
			}
		}
		
		
	};
	
	/*public void initSoundPool() {
		sp = new SoundPool(3, AudioManager.STREAM_MUSIC, 0);
		spMap.put(1,  sp.load(this, R.raw.explode, 1));
	}
	 public void playSound(int sound, int number) {
		 AudioManager am = (AudioManager)this.getSystemService(this.AUDIO_SERVICE);
		 float audioMaxVolumn = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
		 float audioCurrentVolumn = am.getStreamVolume(AudioManager.STREAM_MUSIC);
		 float volumnRatio = audioCurrentVolumn/audioMaxVolumn;
		 sp.play(spMap.get(sound), volumnRatio, volumnRatio, 1, number, 1);
		
	 }
	*/
			
		
}
